package cn.wanda.utils.modelGroup

import cn.wanda.utils.config
import cn.wanda.utils.config.groupJobTrait
import org.apache.spark.sql.{DataFrame, Row, SparkSession}

class group2 extends groupJobTrait {
    val model2TableMap = new config.TableMap
    val model6TableMap = new config.TableMap

    def getGroup(sparkSessionContext: SparkSession): DataFrame = {
        val jdbcDF = sparkSessionContext.sql(group2Sql)
          .rdd
          .map(f = col => {
              val memberCode = col.get(0).toString
              val bqs_90d = isNull(col.get(1))
              val od_tran_cnt_1mth = isNull(col.get(2))
              val shidong_avg_3mon = isNull(col.get(3))
              val total_cnt = isNull(col.get(4))
              val txl_cnt = isNull(col.get(5))
              val shidong_rate = isNull(col.get(6))

              // 计算model2
              var bqs_90d_model2_goal = 0f
              var od_tran_cnt_1mth_model2_goal = 0f
              var shidong_avg_3mon_model2_goal = 0f
              var total_cnt_model2_goal = 0f
              var txl_cnt_model2_goal = 0f

              if (bqs_90d < 0) {
                  bqs_90d_model2_goal = -16.3f
              } else if (bqs_90d == 0) {
                  bqs_90d_model2_goal = 5.4f
              } else {
                  bqs_90d_model2_goal = -32.4f
              }

              if (od_tran_cnt_1mth <= 1) {
                  od_tran_cnt_1mth_model2_goal = 5.9f
              } else if (od_tran_cnt_1mth == 2) {
                  od_tran_cnt_1mth_model2_goal = -23.2f
              } else {
                  od_tran_cnt_1mth_model2_goal = -43.9f
              }

              if (shidong_avg_3mon <= 0.63) {
                  shidong_avg_3mon_model2_goal = 51.0f
              } else if (shidong_avg_3mon > 0.63 && shidong_avg_3mon <= 0.83) {
                  shidong_avg_3mon_model2_goal = 31.4f
              } else if (shidong_avg_3mon > 0.83 && shidong_avg_3mon <= 0.93) {
                  shidong_avg_3mon_model2_goal = 10.7f
              } else if (shidong_avg_3mon > 0.93 && shidong_avg_3mon <= 0.97) {
                  shidong_avg_3mon_model2_goal = -17.0f
              } else {
                  shidong_avg_3mon_model2_goal = -33.0f
              }

              if (total_cnt < 0) {
                  total_cnt_model2_goal = -19.4f
              } else if (total_cnt >= 0 && total_cnt <= 6) {
                  total_cnt_model2_goal = 25.3f
              } else if (total_cnt >= 7 && total_cnt <= 9) {
                  total_cnt_model2_goal = 10.3f
              } else {
                  total_cnt_model2_goal = -18.5f
              }

              if (txl_cnt < 0) {
                  txl_cnt_model2_goal = -21.8f
              } else if (txl_cnt == 0) {
                  txl_cnt_model2_goal = 19.8f
              } else {
                  txl_cnt_model2_goal = -22.6f
              }

              // model2 总分数
              val group2Score: Float = 500f + bqs_90d_model2_goal + od_tran_cnt_1mth_model2_goal + shidong_avg_3mon_model2_goal +
                total_cnt_model2_goal + txl_cnt_model2_goal

              model2TableMap.bqs_90d_value = bqs_90d
              model2TableMap.od_tran_cnt_1mth_value = od_tran_cnt_1mth
              model2TableMap.shidong_avg_3mon_value = shidong_avg_3mon
              model2TableMap.total_cnt_value = total_cnt
              model2TableMap.txl_cnt_value = txl_cnt

              model2TableMap.bqs_90d_goal = bqs_90d_model2_goal
              model2TableMap.od_tran_cnt_1mth_goal = od_tran_cnt_1mth_model2_goal
              model2TableMap.shidong_avg_3mon_goal = shidong_avg_3mon_model2_goal
              model2TableMap.total_cnt_goal = total_cnt_model2_goal
              model2TableMap.txl_cnt_goal = txl_cnt_model2_goal

              model2TableMap.model_of_the_total_score = group2Score

              // 计算group6
              var bqs_90d_model6_goal = 0f
              var od_tran_cnt_1mth_model6_goal = 0f
              var shidong_avg_3mon_model6_goal = 0f
              var shidong_rate_model6_goal = 0f
              var total_cnt_model6_goal = 0f

              if (bqs_90d < 0) {
                  bqs_90d_model6_goal = -22.6f
              } else if (bqs_90d == 0) {
                  bqs_90d_model6_goal = 7.9f
              } else {
                  bqs_90d_model6_goal = -38.3f
              }

              if (od_tran_cnt_1mth <= 1) {
                  od_tran_cnt_1mth_model6_goal = 7.2f
              } else if (od_tran_cnt_1mth == 2) {
                  od_tran_cnt_1mth_model6_goal = -25.5f
              } else {
                  od_tran_cnt_1mth_model6_goal = -51.7f
              }

              if (shidong_avg_3mon <= 0.85) {
                  shidong_avg_3mon_model6_goal = 20.2f
              } else if (shidong_avg_3mon > 0.85 && shidong_avg_3mon <= 0.93) {
                  shidong_avg_3mon_model6_goal = 9.5f
              } else {
                  shidong_avg_3mon_model6_goal = -17.4f
              }

              if (shidong_rate <= 0.80) {
                  shidong_rate_model6_goal = 20.0f
              } else if (shidong_rate > 0.80 && shidong_rate < 0.95) {
                  shidong_rate_model6_goal = 4.0f
              } else {
                  shidong_rate_model6_goal = -15.1f
              }

              if (total_cnt < 0) {
                  total_cnt_model6_goal = -45.7f
              } else if (total_cnt >= 0 && total_cnt <= 6) {
                  total_cnt_model6_goal = 65.2f
              } else if (total_cnt >= 7 && total_cnt <= 9) {
                  total_cnt_model6_goal = 16.0f
              } else {
                  total_cnt_model6_goal = -40.7f
              }

              // group6 总分数
              val group6Score: Float = 500f + bqs_90d_model6_goal + od_tran_cnt_1mth_model6_goal + shidong_avg_3mon_model6_goal +
                shidong_rate_model6_goal + total_cnt_model6_goal

              model6TableMap.bqs_90d_value = bqs_90d
              model6TableMap.od_tran_cnt_1mth_value = od_tran_cnt_1mth
              model6TableMap.shidong_avg_3mon_value = shidong_avg_3mon
              model6TableMap.shidong_rate_value = shidong_rate
              model6TableMap.total_cnt_value = total_cnt

              model6TableMap.bqs_90d_goal = bqs_90d_model6_goal
              model6TableMap.od_tran_cnt_1mth_goal = od_tran_cnt_1mth_model6_goal
              model6TableMap.shidong_avg_3mon_goal = shidong_avg_3mon_model6_goal
              model6TableMap.shidong_rate_goal = shidong_rate_model6_goal
              model6TableMap.total_cnt_goal = total_cnt_model6_goal

              model6TableMap.model_of_the_total_score = group6Score

              // 返回值
              (model2TableMap, model6TableMap)
          })

        val group2Rdd = jdbcDF.map(_._1).map(
            x => Row(x.data_date, 2, x.member_code, null, null, x.txl_cnt_value, x.bqs_90d_value, null, x.total_cnt_value, null,
                x.shidong_avg_3mon_value, null, null, null, null, null, null, null, x.od_tran_cnt_1mth_value, null,
                null, null, null, null, null, null, null, null, x.txl_cnt_goal, x.bqs_90d_goal,
                null, x.total_cnt_goal, null, x.shidong_avg_3mon_goal, null, null, null, null, null, null,
                null, x.od_tran_cnt_1mth_goal, null, null, null, null, null, null, null, x.model_of_the_total_score))

        val group6Rdd = jdbcDF.map(_._2).map(
            x => Row(x.data_date, 6, x.member_code, null, null, null, x.bqs_90d_value, null, x.total_cnt_value, null,
                x.shidong_avg_3mon_value, null, x.shidong_rate_value, null, null, null, null, null, x.od_tran_cnt_1mth_value, null,
                null, null, null, null, null, null, null, null, null, x.bqs_90d_goal,
                null, x.total_cnt_goal, null, x.shidong_avg_3mon_goal, null, x.shidong_rate_goal, null, null, null, null,
                null, x.od_tran_cnt_1mth_goal, null, null, null, null, null, null, null, x.model_of_the_total_score))

        val group2DF = sparkSessionContext.sqlContext.createDataFrame(group2Rdd, schema)
        val group6DF = sparkSessionContext.sqlContext.createDataFrame(group6Rdd, schema)

        group2DF.union(group6DF)
    }
}
